/**
 * Copyright (c) 2017 hadlinks, All Rights Reserved.
 */
package com.bugull.farm.core.control.task.callable;

import com.bugull.farm.core.store.CommandTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Project Name: bugu-farm-household
 * Package Name: com.bugull.farm.device.shortconn.server.control.task.callable 
 * ClassName: HumSetCallable 
 * Function: TODO ADD FUNCTION.  
 * date: 2017/10/28 17:26
 * @author songwei (songw@hadlinks.com)
 * @since JDK 1.8 
 */
public class HumSetCallable extends UpperControlCallable {

    private Logger logger = LogManager.getLogger(UpperControlCallable.class);

    public HumSetCallable(CommandTask commandTask) {
        super(commandTask);
    }

    @Override
    public Boolean call() throws Exception {

////        //查找设备连接信息
////        String terminalSN = commandTask.getTerminalSN();
////        DeviceCache cache = DeviceCache.getInstance();
////        String channelId = cache.getChannelId(terminalSN);
////        channelId = channelId == null ? "" : channelId;
////        ChannelHandlerContext ctx = cache.getContext(channelId);
////        if (ctx != null) { //检查连接状态，离线不执行任务
////            //拼装下发的命令
////            //命令产生的时间
////            int date = (int) (commandTask.getDate().getTime() / 1000L);
////            //温度上限
////            byte[] maxTemp = new byte[2];
////            String maxTempStr = commandTask.getTempSetting().getMaxTemp1() + "";
////            maxTemp[0] = Integer.valueOf(maxTempStr.split("\\.")[0]).byteValue();
////            maxTemp[1] = Integer.valueOf(maxTempStr.split("\\.")[1]).byteValue();
////            //温度下限
////            byte[] minTemp = new byte[2];
////            String minTempStr = commandTask.getTempSetting().getMinTemp1() + "";
////            minTemp[0] = Integer.valueOf(minTempStr.split("\\.")[0]).byteValue();
////            minTemp[1] = Integer.valueOf(minTempStr.split("\\.")[1]).byteValue();
////
////            try {
////                if (commandTask.getHumComplete() == null || !commandTask.getHumComplete()) {
////                    //湿度信息设置
////                    ByteBuf humSet = Unpooled.buffer();
////                    humSet.writeByte(props.getByte("pv.short"));//pv
////                    humSet.writeByte(props.getByte("byte.blank"));//flag
////                    humSet.writeShort(props.getInt("byte.blank"));//len
////                    humSet.writeByte(CommandEnum.CMD_DEV_SHORTCONN_HUM_SET.getCmd());//cmd
////                    humSet.writeInt(date);//Date
////                    humSet.writeByte(commandTask.getHumSetting().getMaxHum());//MaxHum
////                    humSet.writeByte(commandTask.getHumSetting().getMinHum());//MinHum
////                    //重新计算长度
////                    humSet.setShort(2, humSet.readableBytes() - 4);
////                    ctx.writeAndFlush(humSet).addListener(new ChannelFutureListener() {
////                        @Override
////                        public void operationComplete(ChannelFuture future) throws Exception {
////                            if (future.isSuccess()) {
////                                String message = commandTask.getHumSetting().getMaxHum()
////                                        + "," + commandTask.getHumSetting().getMaxHum();
////                                //下发成功，缓存命令信息
////                                cache.addHum(terminalSN, message);
////                                logger.debug("设备{}成功下发湿度设置指令{}!!!",
////                                        terminalSN, message);
////                            }
////                        }
////                    });
////                }
//            } catch (Exception e) {
//                logger.error("设备{}下发控制指令失败!!!", terminalSN, e);
//                return Boolean.FALSE;
//            }
//        } else {
//            return Boolean.FALSE;
//        }
        return Boolean.TRUE;
    }

    public CommandTask getCommandTask() {
        return commandTask;
    }

    public Long getCreateTime() {
        return createTime;
    }
}
